package ipl.projet.servlets;

import ipl.projet.exceptions.PartieInconnueException;
import ipl.projet.usecase.UccStarTrek;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Journal extends HttpServlet implements Servlet {
	private static final long serialVersionUID = 1L;

	@EJB
	private UccStarTrek ucc;
	
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession sessionTEST = request.getSession();
		if (sessionTEST == null || sessionTEST.getAttribute("login") == null) {
			response.sendRedirect(response
					.encodeRedirectURL("connexion.html?erreur=1"));
			return;
		}
		
		String journal = null;
		try {
			journal = ucc.afficherJournal(request.getParameter("partie"));
		} catch (PartieInconnueException e) {
			response.sendRedirect(response.encodeRedirectURL("accueil.html?erreur=3"));
			return;
		}
		
		request.setAttribute("journal", journal);
		getServletContext().getNamedDispatcher("journal.html").forward(request,
				response);
	}
	
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}
}
